package io.renren.modules.nuaa.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.utils.image.ImageTool;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.nuaa.dto.TCategoryDTO;
import io.renren.modules.nuaa.dto.TCertificateDTO;
import io.renren.modules.nuaa.dto.TQuestionDTO;
import io.renren.modules.nuaa.entity.AllContentEntity;
import io.renren.modules.nuaa.excel.TCertificateExcel;
import io.renren.modules.nuaa.listener.TCertificateListener;
import io.renren.modules.nuaa.service.TCategoryService;
import io.renren.modules.nuaa.service.TCertificateService;
import io.renren.modules.sys.dto.SysUserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
* 证书表
*
* @author Mark sunlightcs@gmail.com
* @since 3.0 2023-11-07
*/
@RestController
@RequestMapping("nuaa/tcertificate")
@Api(tags="证书表")
public class TCertificateController {
    @Autowired
    private TCertificateService tCertificateService;
    @Autowired
    private TCategoryService tCategoryService;

    @Autowired
    private TCertificateListener tCertificateListener;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    @RequiresPermissions("nuaa:tcertificate:page")
    public Result<PageData<TCertificateDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<TCertificateDTO> page = tCertificateService.page(params);

        List<TCertificateDTO> tCertificateDTOList = page.getList();

        tCertificateDTOList.forEach(dto ->{

            TCategoryDTO tCategoryDTO = tCategoryService.get(dto.getCategoryId().longValue());
            dto.setCategoryName(tCategoryDTO.getCategoryName());
        });


        return new Result<PageData<TCertificateDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("nuaa:tcertificate:info")
    public Result<TCertificateDTO> get(@PathVariable("id") Long id){
        TCertificateDTO data = tCertificateService.get(id);

        return new Result<TCertificateDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("nuaa:tcertificate:save")
    public Result save(@RequestBody TCertificateDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        tCertificateService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("nuaa:tcertificate:update")
    public Result update(@RequestBody TCertificateDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        tCertificateService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("nuaa:tcertificate:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        tCertificateService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("nuaa:tcertificate:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<TCertificateDTO> list = tCertificateService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "证书表", list, TCertificateExcel.class);
    }

    /**
     * 导入题目表格
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("import")
    @ApiOperation("导入")
    @ApiImplicitParam(name = "file", value = "文件", paramType = "query", dataType="file")
    public Result importExcel(@RequestParam("file") MultipartFile file) throws Exception {

        ExcelReaderBuilder readCertificate = EasyExcel.read(file.getInputStream(), TCertificateExcel.class,tCertificateListener);

        ExcelReaderSheetBuilder sheet = readCertificate.sheet();

        sheet.doRead();

        return new Result();
    }

    @PostMapping("generateCer")
    public Result generateCer(@RequestBody List<TCertificateDTO> list) throws Exception{

        list.stream()
                .filter(tCertificateDTO -> tCertificateDTO.getCertificateNo() == null || tCertificateDTO.getCertificateNo() == "")
                .forEach( t->{
                    System.out.println(t.getId());
                    AllContentEntity rongyu = ImageTool.rongyu("EDECO-2021-0001",
                            t.getName(),
                            "测试大赛",
                            "低年级组",
                            "一等奖",
                            "第一小学",
                            "张老师",
                            "http://localhost/img/1700460482251.jpg",
                            "http://localhost/img/certificategroundback.jpg");
                    String url = ImageTool.drawList(rongyu);

                    t.setImg(url);
                    t.setCertificateNo("EDECO-2021-0001");

                    tCertificateService.update(t);

                });




        return new Result();
    }

}